﻿using FileUpload.Entity;
using FileUpload.ViewModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace FileUpload.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ReceivingAddressManagementController : ControllerBase
    {

        public static List<ReceivingAddressManagement> receivingAddressManagements { get; set; } = new List<ReceivingAddressManagement>();

        [HttpGet]
        public dynamic Get([FromQuery] SelectInput selectInput)
        {
            return new
            {
                total = receivingAddressManagements.Count(),
                data = receivingAddressManagements.Skip((selectInput.pageIndex - 1) * selectInput.pageSize).Take(selectInput.pageSize).ToList()
            };
        }

        [HttpGet("{Id}")]
        public ReceivingAddressManagement? Get(Guid Id)
        {
            return receivingAddressManagements.FirstOrDefault(x => x.Id == Id);
        }


        [HttpDelete("{Id}")]
        public void Delete(Guid Id)
        {
            ReceivingAddressManagement? receivingAddressManagement1 = receivingAddressManagements.FirstOrDefault(x => x.Id == Id);
            if (receivingAddressManagement1 != null)
                receivingAddressManagements.Remove(receivingAddressManagement1);
        }

        /// <summary>
        /// 上传数据
        /// </summary>
        /// <param name="uploadStuInfoInput"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Post(ReceivingAddressManagement receivingAddressManagement)
        {
            if (receivingAddressManagement.Id.HasValue)
            {
                ReceivingAddressManagement? receivingAddressManagement1 = receivingAddressManagements.FirstOrDefault(x => x.Id == receivingAddressManagement.Id);
                if (receivingAddressManagement1 != null)
                    receivingAddressManagements.Remove(receivingAddressManagement1);
            }
            receivingAddressManagement.Id = Guid.NewGuid();
            receivingAddressManagements.Add(receivingAddressManagement);
            return Ok("保存成功!");

        }
    }
}
